
/*增減*/
function plus(flag, id, name, smallImage, price, productSkuRelId){
	var itemCountId = "#itemCounts"+id+""+productSkuRelId;
	var curCounts = $(itemCountId).text();
	var curCountsInt = parseInt(curCounts);
	if(flag == 1){
		sendData(id, name, smallImage, price, 1, productSkuRelId);
	}else if(flag == 0){
		if(curCountsInt > 1){
			sendData(id, name, smallImage, price, -1, productSkuRelId);
		}
	}
}

function sendData(id, name, smallImage, price, counts, productSkuRelId){
	var itemCountId = "#itemCounts"+id+""+productSkuRelId;
	var itemValueId = "#itemValue"+id+""+productSkuRelId;
	$.ajax({
		type : 'POST',
		url : ctx + '/shop/addProductToCart.do',
		data : {
			productId : id,
			productName : name,
			smallImage : smallImage,
			counts : counts,
			price : price,
			productSkuRelId : productSkuRelId
		},
		dataType : 'json',
		timeout : 30000,
		context : $('body'),
		success : function(data) {
			if (data.code == "ok") {
				var obj = data.object;
				//小计
				var items = obj.items;
				for (item in items) {
					item = items[item];
					if(item.productId == id && item.productSkuRelId == productSkuRelId){
						$(itemCountId).text(item.counts);
						$(itemValueId).text(item.value.toFixed(2));
					}
				}
				//总计
				$("#totalCounts").text(obj.counts);
				$("#totalValue1").text(obj.value.toFixed(2));
				$("#totalValue2").text(obj.value.toFixed(2));
				
			} else {
				showTip2(data.msg);
			}
		},
		error : function(xhr, type) {
			showTip2('提交失败!');
		}
	});
}

/*全选全不选*/
$(document).ready(function() {
	$("#all").click(function(){
		$("[name=items]:checkbox").attr("checked",this.checked);
	});
});

/*去结算*/
function goAccount(){
	window.location.href = decodeURIComponent(ctx + '/order/orderAccount');
}

/*删除选中的*/
function delItems(){
	//输出选中的值
	var prdtIdStr="";
	var relIdStr="";
	$("[name=items]:checkbox:checked").each(function(){
		var productId = $(this).val();
		var relId = $(this).attr("relId");
		prdtIdStr+=productId+",";
		relIdStr+=relId+",";
		var mediaId = "#media"+productId+relId;
		//删除条目
		$(mediaId).remove();
	});
	
	$.ajax({
		type : 'POST',
		url : ctx + '/shop/deleteCartItems.do',
		data : {
			productIds : prdtIdStr,
			productSkuRelIds : relIdStr
		},
		dataType : 'json',
		timeout : 30000,
		context : $('body'),
		success : function(data) {
			if (data.code == "ok") {
				var obj = data.object;
				//总计
				$("#totalCounts").text(obj.counts);
				$("#totalValue1").text(obj.value.toFixed(2));
				$("#totalValue2").text(obj.value.toFixed(2));
			} else {
				showTip2(data.msg);
			}
		},
		error : function(xhr, type) {
			showTip2('提交失败!');
		}
	});
}

